package org.example.votingsystem.dao;

import org.example.votingsystem.items.YearPeople;
import org.example.votingsystem.util.SqlManege;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class YearPeopleDao {
    public ArrayList<YearPeople> searchYearPeople() throws SQLException {
        ArrayList<YearPeople> arrayList = new ArrayList<>();
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * \n" +
                "FROM yearPeople");
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            YearPeople yearPeople = new YearPeople();
            yearPeople.setAccount(resultSet.getInt("account"));
            yearPeople.setName(resultSet.getString("name"));
            yearPeople.setSex(resultSet.getString("sex"));
            yearPeople.setInfo(resultSet.getString("info"));
            yearPeople.setCounts(resultSet.getInt("counts"));
            arrayList.add(yearPeople);
        }
        return arrayList;
    }
//    删除某个年度人物

    public void deleteYearPeople(String account) throws SQLException {
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM yearPeople WHERE account = ?");
        preparedStatement.setString(1, account);
        preparedStatement.executeUpdate();
        sqlManege.disconnect(connection, preparedStatement);
    }
//    插入某个年度人物


    public void insertYearPeople(YearPeople yearPeople) throws SQLException {
        SqlManege sqlManege = new SqlManege();
        Connection connection = sqlManege.connect();
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO yearPeople (account, name, sex, info, counts) VALUES (?,?,?,?,?)");
        preparedStatement.setInt(1, yearPeople.getAccount());
        preparedStatement.setString(2, yearPeople.getName());
        preparedStatement.setString(3, yearPeople.getSex());
        preparedStatement.setString(4, yearPeople.getInfo());
        preparedStatement.setInt(5, yearPeople.getCounts());
        preparedStatement.executeUpdate();
        sqlManege.disconnect(connection, preparedStatement);
    }


}
